package com.iniapp.app.web.action.admin;


import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.iniapp.app.web.action.BaseCtrl;
import com.inibb.app.service.admin.ISaSystemParamService;
import com.inibb.app.vo.SysParamSC;
import com.inibb.app.vo.admin.SaSystemParamVO;

@Controller("sysParamCtrl")
@Scope("session")//page
public class SysParamCtrl extends BaseCtrl  implements Serializable{

	private static final long serialVersionUID = 1L;
	
	@Autowired
	private ISaSystemParamService saSystemParamService;
	
	private LazyDataModel<SaSystemParamVO> listModel;
	//private String currentType;
	private SaSystemParamVO selectedRecord;
	private SaSystemParamVO editRecord = new SaSystemParamVO();
	private LazyDataModel<SaSystemParamVO> selectedRecords;

	@PostConstruct
	public void init(){
		SysParamSC paginationCriteria = new SysParamSC();
		paginationCriteria.setStartRow(0);
		paginationCriteria.setPageSize(10);
		listModel = searchParams(paginationCriteria);
		
	}

	private LazyDataModel<SaSystemParamVO> searchParams(final SysParamSC paginationCriteria){
		LazyDataModel<SaSystemParamVO> clm = new LazyDataModel<SaSystemParamVO>() {
			private static final long serialVersionUID = 1L;

			@Override
			public List<SaSystemParamVO> load(int rowStart, int pageSize, String arg2, SortOrder sorOder, Map<String, String> arg4) {
				paginationCriteria.setStartRow(rowStart);
				paginationCriteria.setPageSize(pageSize);
				
				return saSystemParamService.searchPageSystemParams(paginationCriteria);
			}
			
			@Override  
		    public SaSystemParamVO getRowData(String rowKey) {  
		        //In a real app, a more efficient way like a query by rowKey should be implemented to deal with huge data  
//				return saSystemParamService.getCode(Integer.parseInt(rowKey));
		        List<SaSystemParamVO> cars = (List<SaSystemParamVO>) this.getWrappedData();  
		        for(SaSystemParamVO car : cars) {  
		            if(car.getParamId().toString().equals(rowKey))  
		                return car;  
		        }  
		          
		        return null;  
		    }  
		  
		    @Override  
		    public String getRowKey(SaSystemParamVO car) {  
		        return car.getParamId().toString();
		    }  
		};
		int total = saSystemParamService.countPageSystemParams(paginationCriteria);
		clm.setRowCount(total);
		return clm;
	}
	
	public void create(){
		editRecord = new SaSystemParamVO();
	}
	
	public void edit(){
		editRecord = getSelectedRecord();
	}
	
	public void saveEdit(){
		saSystemParamService.saveOrUpdate(editRecord);
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "保存记录成功！",  "保存记录成功！");  
        FacesContext.getCurrentInstance().addMessage("保存记录成功！", message);  
	}
	
	public void deleteSelected(){
		saSystemParamService.delete(selectedRecord);
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "删除记录成功！",  "删除记录成功！");  
        FacesContext.getCurrentInstance().addMessage("删除记录成功！", message);  
	}

	public LazyDataModel<SaSystemParamVO> getListModel() {
		return listModel;
	}

	public void setListModel(LazyDataModel<SaSystemParamVO> listModel) {
		this.listModel = listModel;
	}

	public SaSystemParamVO getSelectedRecord() {
		return selectedRecord;
	}

	public void setSelectedRecord(SaSystemParamVO selectedRecord) {
		this.selectedRecord = selectedRecord;
	}

	public SaSystemParamVO getEditRecord() {
		return editRecord;
	}

	public void setEditRecord(SaSystemParamVO editRecord) {
		this.editRecord = editRecord;
	}

	public LazyDataModel<SaSystemParamVO> getSelectedRecords() {
		return selectedRecords;
	}

	public void setSelectedRecords(LazyDataModel<SaSystemParamVO> selectedRecords) {
		this.selectedRecords = selectedRecords;
	}
	
}
